#Include "../common/log.ahk"
#Include "../common/bt.ahk"
#SingleInstance Force
SendMode "Input"
SetKeyDelay 0

Init()

esIni := FileExist(iniPath)
if(esIni) {
    ConfigRead()
} else
{
    InIWriter()
}
myGui()

F1:: 
    {
        MouseGetPos &MouseX, &MouseY
        color := PixelGetColor(MouseX, MouseY)
        obj.color:=color
        obj.x:=MouseX
        obj.y:=MouseY
        InIWriter()
    }

    ;初始化
    Init() 
    {
        global codeKey := "XButton2"
        global iniPath := "./ck.ini"
        global common:= {
            keyModel : 0
        }
        global obj := {
            rKey:52,
            tKey:23,
            color:"",
            x:0,
            y:0,
            keyModel:0
        }
        global obj0 := {
            tKey: 50
        }

        global obj1 := {
            tKey:50,
            xKey:50
        }

        global ed := Map("m0TE", "m1XE")
    }

    ;写入配置文件
    InIWriter() 
    {
        IniWrite common.keyModel, iniPath, "keyModel", "keyModel"
        ;
        IniWrite obj.rKey, iniPath, "ck", "rKey"
        IniWrite obj.tKey, iniPath, "ck", "tKey"
        IniWrite obj.color, iniPath, "ck", "color"
        IniWrite obj.x, iniPath, "ck", "x"
        IniWrite obj.y, iniPath, "ck", "y"

        ;=========model0
        IniWrite obj0.tKey, iniPath, "ck0", "tKey"

        ;=========model1
        IniWrite obj1.tKey, iniPath, "ck1", "tKey"
        IniWrite obj1.xKey, iniPath, "ck1", "xKey"
        ;MsgBox common.keyModel
        WriterLog("写入配置")
    }

    ;读取配置文件
    ConfigRead() 
    {
        common.keyModel := IniRead(iniPath, "keyModel", "keyModel")
        obj.rKey := IniRead(iniPath, "ck", "rKey")
        obj.tKey := IniRead(iniPath, "ck", "tKey")
        obj.color := IniRead(iniPath, "ck", "color")
        obj.x := IniRead(iniPath, "ck", "x")
        obj.y := IniRead(iniPath, "ck", "y")
        ; model0
        obj0.tKey := IniRead(iniPath, "ck0", "tKey", "50")
        ; model1
        obj1.tKey := IniRead(iniPath, "ck1", "tKey", "50")
        obj1.xKey := IniRead(iniPath, "ck1", "xKey", "50")
        WriterLog("读取到配置")
    }

    ; 写入日志
    WriterLog(name) 
    {
        str:=name "：rKey:" obj.rKey " tKey:" obj.tKey " x:" obj.x " y:" obj.y `n" color:" obj.color " keyModel:" common.keyModel
        Log.log(&str)
    }

    myGui() 
    {
        MyGui := Gui(,"刺客宏")
        MyGui.Add("Text", "ym+7 xm+0 Section", "模式1 刺心模式; `n `n模式2 震天雷模式`n ")
        MyGui.Add("Text", "ym+47 xm+0 Section", "选择按键后，点击重新加载触发配置`n ")
        MyGui.Add("Text", "xs")
        MyGui.Add("Text", "ym+70 xm+0 Section", "卡刀按键类型：")
        global b1Radio:=MyGui.Add("Radio","ym+70 x+2 vb1Radio", "模式1")
        b1Radio.OnEvent("Click", Ctrl_Click)
        global b2Radio:=MyGui.Add("Radio","ym+70 x+2 vb2Radio", "模式2")
        b2Radio.OnEvent("Click", Ctrl_Click)
        if(common.keyModel==0) {
            b1Radio.value:=1
            b2Radio.value:=0
        } else if(common.keyModel==1) {
            b1Radio.value:=0
            b2Radio.value:=1
        } 
        MyGui.Add("Text", "xs")

        MyGui.Add("Text",, "宏定义")
        if(common.keyModel==0) {
            MyGui.Add("Text","y+5 xm+14 Section", "T:") ; 保存此控件位置并定义一个新控件段.
            m0TE := MyGui.Add("Edit", "vm0TE x+30 w50")
            m0TE.value:=obj0.tKey
            m0TE.OnEvent("Change", Ctrl_Change)
        } else if(common.keyModel==1) {
            MyGui.Add("Text","y+5 xm+14 Section", "T:") ; 保存此控件位置并定义一个新控件段.
            m1TE := MyGui.Add("Edit", "vm1TE x+30 w50")
            m1TE.value:=obj1.tKey
            m1TE.OnEvent("Change", Ctrl_Change)
            MyGui.Add("Text", "y+5 xm+15", "X:")
            m1XE := MyGui.Add("Edit", "vm1XE x+30 w50")
            m1XE.value:=obj1.xKey
            m1XE.OnEvent("Change", Ctrl_Change)
        } else {
            MyGui.Add("Text","y+5 xm+14 Section", "R:") ; 保存此控件位置并定义一个新控件段.
            global rEdit := MyGui.Add("Edit", "x+30 w50")
            rEdit.value:=obj.rKey

            MyGui.Add("Text", "y+5 xm+15", "T:")
            global tEdit := MyGui.Add("Edit", "x+30 w50")
            tEdit.value:=obj.tKey

            MyGui.Add("Text", "y+5 xm+14", "X:")
            MyGui.Add("Text", "x+30", obj.x)
            MyGui.Add("Text", "y+5 xm+14", "Y:")
            MyGui.Add("Text", "x+30", obj.y)
            MyGui.Add("Text", "y+5 xm+0", "取色值:" )
            MyGui.Add("Text", "x+5", obj.color)
        }

        MyGui.Add("Text", "xs")
        MyBtn := MyGui.AddButton("w80", "重新加载")
        MyBtn.OnEvent("Click", loadAhk) ; 当点击时, 调用 MyBtn_Click.
        MyGui.OnEvent("Close", ProcessUserInput)
        MyGui.Restore()
        MyGui.Show()
    }

    Ctrl_Click(GuiCtrlObj, Info)
    {
        if(GuiCtrlObj.Name == 'b1Radio') {
            if(GuiCtrlObj.Value==1) {
                common.keyModel:=0
            }
        } else if(GuiCtrlObj.Name == 'b2Radio') {
            if(GuiCtrlObj.Value==1) {
                common.keyModel:=1
            }
        }
        ;MsgBox common.keyModel
        InIWriter()
        Reload
    }

    Ctrl_Change(GuiCtrlObj, Info) {
        if(GuiCtrlObj.Name == 'm0TE') {
            obj0.tKey:=GuiCtrlObj.value
        } else if(GuiCtrlObj.Name == 'm1TE') {
            obj1.tKey:=GuiCtrlObj.value
        } else if(GuiCtrlObj.Name == 'm1XE') {
            obj1.xKey:=GuiCtrlObj.value
        }
    }

    loadAhk(*) 
    {
        InIWriter()
        Reload
    }

    if(common.keyModel == 0) {
        Hotkey codeKey, model0
    } else if(common.keyModel == 1) {
        Hotkey codeKey, model1
    }
    Hotkey "XButton1",ssCode

    model0(ThisHotkey) {
        While GetKeyState(codeKey,"P") {
            SendInput "{t}"
            Sleep obj0.tKey
        } 
    }
    model1(ThisHotkey) {
        While GetKeyState(codeKey,"P") {
            SendInput "{t}"
            Sleep obj1.tKey
            SendInput "{x}"
            Sleep obj1.xKey
        } 
    }

    model3(ThisHotkey)
    {
        While GetKeyState(codeKey,"P") {
            SendInput "{r}"
            color := PixelGetColor(obj.x, obj.y)
            Sleep obj.rKey
            if(obj.color!=color) {
                SendInput "{t}"
                Sleep obj.tKey
            }
        }
    }

    ssCode(ThisHotkey) 
    {
        SendInput "{s}"
        Sleep 50
        SendInput "{s}"
    }
ProcessUserInput(thisGui, *)
{
  
    ;;MsgBox("宏已被最小话")
}